package com.neusoft.controller;

import com.neusoft.mapper.ProductMapper;
import com.neusoft.po.CommonResult;
import com.neusoft.po.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductMapper productMapper;

    // 获取所有商品
    @GetMapping
    public CommonResult getAllProducts() {
        List<Product> products = productMapper.getAllProducts();
        return new CommonResult(200, "success", products);
    }

    // 根据 productId 获取商品
    @GetMapping("/{productId}")
    public CommonResult getProductById(@PathVariable("productId") Integer productId) {
        Product product = productMapper.getProductById(productId);
        if (product == null) {
            return new CommonResult(404, "Product not found", null);
        }
        return new CommonResult(200, "success", product);
    }
}